package com.zyy.smartcity1129.movie;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.util.RvAd;
import com.zyy.smartcity1129.data.MovieFilmInfo;
import com.zyy.smartcity1129.data.MovieSessions;
import com.zyy.smartcity1129.data.MovieTheatres;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @ClassName: MovieTheatreFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 22/11/2021$
 * @Version: 1.0
 */
public class MovieTheatreFragment extends BaseFragment {
    private String theatreName = MovieMovieFragment.movieTheatreName;
    private int theatreId = MovieMovieFragment.movieTheatreId;
    ExecutorService executor = Executors.newFixedThreadPool(3);
    MutableLiveData<AtomicInteger> mData = new MutableLiveData<>();
    AtomicInteger mInteger = new AtomicInteger(0);
    private TextView tvMovieTheatre1;
    private TextView tvMovieTheatre2;
    private ImageView imgMovieTheatre1;
    private LinearLayout lineMovieTheatre3;
    private ImageView imgMovieTheatre5;
    private ImageView imgMovieTheatre6;
    private ImageView imgMovieTheatre7;
    private LinearLayout lineMovieTheatre1;
    private ImageView imgMovieTheatre2;
    private ImageView imgMovieTheatre3;
    private LinearLayout lineMovieTheatre2;
    private ImageView imgMovieTheatre4;
    private TextView tvMovieTheatre4;
    private TextView tvMovieTheatre5;
    private TextView tvMovieTheatre6;
    private TextView tvMovieTheatreDate1;
    private TextView tvMovieTheatreDate2;
    private TextView tvMovieTheatreDate3;
    private RecyclerView rvMovieTheatre;
    List<MovieSessions.RowsBean> sessionList = new ArrayList<>();
    MovieTheatres.DataBean dataBean;
    List<MovieFilmInfo.RowsBean> filmList = new ArrayList<>();
    RvAd<MovieSessions.RowsBean> rvAd;
    List<MovieSessions.RowsBean> movieTheatres = new ArrayList<>();
    String mDate;
    public static int movieRoomId;
    public static int movieMovieId;
    public static int movieTheaterId;
    public static int movieTimesId;
    public static double moviePrice;
    public static String moviePlayDate;

    @Override
    protected int layId() {
        return R.layout.fragment_movie_theatre;
    }

    @Override
    protected String title() {
        return theatreName;
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View v) {
        initView(v);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                dataBean = Http.getInstance().get("/prod-api/api/movie/theatre/" + theatreId, null, MovieTheatres.class, 1).getData();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        executor.execute(new Runnable() {
            @Override
            public void run() {
                filmList = Http.getInstance().get("/prod-api/api/movie/film/list", null, MovieFilmInfo.class, 1).getRows();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        executor.execute(new Runnable() {
            @Override
            public void run() {
                sessionList = Http.getInstance().get("/prod-api/api/movie/theatre/times/list", null, MovieSessions.class, 1).getRows();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        mData.observe(this, atomicInteger -> {
            if (mInteger.get() == 3) {
                List<MovieSessions.RowsBean> sessions = new ArrayList<>();
                sessions.addAll(sessionList.stream().filter(a -> a.getTheaterId() == theatreId).collect(Collectors.toList()));
                tvMovieTheatre1.setText(dataBean.getName());
                tvMovieTheatre2.setText(dataBean.getArea() + dataBean.getAddress());
                Glide.with(imgMovieTheatre1.getContext()).load(Http.base + dataBean.getCover()).into(imgMovieTheatre1);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");
                String date = simpleDateFormat.format(new Date());
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(new Date());
                calendar.add(Calendar.DATE,1);
                String date2= simpleDateFormat.format(calendar.getTime());
                Calendar calendar2 = new GregorianCalendar();
                calendar2.setTime(new Date());
                calendar2.add(Calendar.DATE,2);
                String date3= simpleDateFormat.format(calendar2.getTime());
                tvMovieTheatreDate1.setText(date);
                tvMovieTheatreDate2.setText(date2);
                tvMovieTheatreDate3.setText(date3);
                mDate = date;
                tvMovieTheatreDate1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvMovieTheatreDate1.setTextColor(getResources().getColor(R.color.sell));
                        tvMovieTheatreDate2.setTextColor(getResources().getColor(R.color.gray));
                        tvMovieTheatreDate3.setTextColor(getResources().getColor(R.color.gray));
                        mDate = date;
                    }
                });
                tvMovieTheatreDate2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvMovieTheatreDate2.setTextColor(getResources().getColor(R.color.sell));
                        tvMovieTheatreDate1.setTextColor(getResources().getColor(R.color.gray));
                        tvMovieTheatreDate3.setTextColor(getResources().getColor(R.color.gray));
                        mDate = date2;
                    }
                });
                tvMovieTheatreDate3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvMovieTheatreDate3.setTextColor(getResources().getColor(R.color.sell));
                        tvMovieTheatreDate1.setTextColor(getResources().getColor(R.color.gray));
                        tvMovieTheatreDate2.setTextColor(getResources().getColor(R.color.gray));
                        mDate = date3;
                    }
                });

                List<Integer> movieIds = new ArrayList<>();
                for (int i = 0; i < sessions.size(); i++) {
                    movieIds.add(sessions.get(i).getMovieId());
                }
                List<MovieFilmInfo.RowsBean> comList = new ArrayList<>();
                for (int movieId : movieIds) {
                    comList.addAll(filmList.stream().filter(a -> a.getId() == movieId).collect(Collectors.toList()));
                }
                if (sessions.size() == 1) {
                    lineMovieTheatre1.setVisibility(View.GONE);
                    lineMovieTheatre3.setVisibility(View.GONE);
                    lineMovieTheatre2.setVisibility(View.VISIBLE);
                    Glide.with(imgMovieTheatre4.getContext()).load(Http.base + comList.get(0).getCover()).into(imgMovieTheatre4);
                } else if (sessions.size() == 2) {
                    lineMovieTheatre1.setVisibility(View.VISIBLE);
                    lineMovieTheatre3.setVisibility(View.GONE);
                    lineMovieTheatre2.setVisibility(View.GONE);
                    Glide.with(imgMovieTheatre2.getContext()).load(Http.base + comList.get(0).getCover()).into(imgMovieTheatre2);
                    Glide.with(imgMovieTheatre3.getContext()).load(Http.base + comList.get(1).getCover()).into(imgMovieTheatre3);
                } else if (sessions.size() == 3){
                    lineMovieTheatre1.setVisibility(View.GONE);
                    lineMovieTheatre3.setVisibility(View.VISIBLE);
                    lineMovieTheatre2.setVisibility(View.GONE);
                    Glide.with(imgMovieTheatre5.getContext()).load(Http.base + comList.get(0).getCover()).into(imgMovieTheatre5);
                    Glide.with(imgMovieTheatre6.getContext()).load(Http.base + comList.get(1).getCover()).into(imgMovieTheatre6);
                    Glide.with(imgMovieTheatre7.getContext()).load(Http.base + comList.get(2).getCover()).into(imgMovieTheatre7);
                }
                if (sessions.size() == 0){
                    tvMovieTheatre4.setVisibility(View.GONE);
                    tvMovieTheatre5.setVisibility(View.GONE);
                    tvMovieTheatre6.setVisibility(View.GONE);
                    lineMovieTheatre2.setVisibility(View.GONE);
                    lineMovieTheatre1.setVisibility(View.GONE);
                    lineMovieTheatre3.setVisibility(View.GONE);
                    rvMovieTheatre.setVisibility(View.GONE);
                    tvMovieTheatreDate1.setVisibility(View.GONE);
                    tvMovieTheatreDate2.setVisibility(View.GONE);
                    tvMovieTheatreDate3.setVisibility(View.GONE);
                } else {
                    tvMovieTheatre4.setText(comList.get(0).getName());
                    tvMovieTheatre5.setText(comList.get(0).getScore() + "分");
                    tvMovieTheatre6.setText(comList.get(0).getDuration() + "分钟 | " + comList.get(0).getLanguage());
                    rvMovieTheatre.setLayoutManager(new LinearLayoutManager(getContext()));
                    movieTheatres.addAll(sessions.stream().filter(a -> a.getMovieId() == comList.get(0).getId()).collect(Collectors.toList()));
                    initAdapter(movieTheatres);
                }

                imgMovieTheatre2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Glide.with(imgMovieTheatre2.getContext()).load(Http.base + comList.get(0).getCover()).into(imgMovieTheatre2);
                        Glide.with(imgMovieTheatre3.getContext()).load(Http.base + comList.get(1).getCover()).into(imgMovieTheatre3);
                        tvMovieTheatre4.setText(comList.get(0).getName());
                        tvMovieTheatre5.setText(comList.get(0).getScore() + "分");
                        tvMovieTheatre6.setText(comList.get(0).getDuration() + "分钟 | " + comList.get(0).getLanguage());
                        movieTheatres.clear();
                        movieTheatres.addAll(sessions.stream().filter(a -> a.getMovieId() == comList.get(0).getId()).collect(Collectors.toList()));
                        initAdapter(movieTheatres);
                    }
                });
                imgMovieTheatre3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Glide.with(imgMovieTheatre2.getContext()).load(Http.base + comList.get(1).getCover()).into(imgMovieTheatre2);
                        Glide.with(imgMovieTheatre3.getContext()).load(Http.base + comList.get(0).getCover()).into(imgMovieTheatre3);
                        tvMovieTheatre4.setText(comList.get(1).getName());
                        tvMovieTheatre5.setText(comList.get(1).getScore() + "分");
                        tvMovieTheatre6.setText(comList.get(1).getDuration() + "分钟 | " + comList.get(1).getLanguage());
                        movieTheatres.clear();
                        movieTheatres.addAll(sessions.stream().filter(a -> a.getMovieId() == comList.get(1).getId()).collect(Collectors.toList()));
                        initAdapter(movieTheatres);
                    }
                });
                imgMovieTheatre5.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Glide.with(imgMovieTheatre5.getContext()).load(Http.base + comList.get(0).getCover()).into(imgMovieTheatre5);
                        Glide.with(imgMovieTheatre6.getContext()).load(Http.base + comList.get(1).getCover()).into(imgMovieTheatre6);
                        Glide.with(imgMovieTheatre7.getContext()).load(Http.base + comList.get(2).getCover()).into(imgMovieTheatre7);
                        tvMovieTheatre4.setText(comList.get(0).getName());
                        tvMovieTheatre5.setText(comList.get(0).getScore() + "分");
                        tvMovieTheatre6.setText(comList.get(0).getDuration() + "分钟 | " + comList.get(0).getLanguage());
                        movieTheatres.clear();
                        movieTheatres.addAll(sessions.stream().filter(a -> a.getMovieId() == comList.get(0).getId()).collect(Collectors.toList()));
                        initAdapter(movieTheatres);
                    }
                });
                imgMovieTheatre6.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Glide.with(imgMovieTheatre5.getContext()).load(Http.base + comList.get(1).getCover()).into(imgMovieTheatre5);
                        Glide.with(imgMovieTheatre6.getContext()).load(Http.base + comList.get(2).getCover()).into(imgMovieTheatre6);
                        Glide.with(imgMovieTheatre7.getContext()).load(Http.base + comList.get(0).getCover()).into(imgMovieTheatre7);
                        tvMovieTheatre4.setText(comList.get(1).getName());
                        tvMovieTheatre5.setText(comList.get(1).getScore() + "分");
                        tvMovieTheatre6.setText(comList.get(1).getDuration() + "分钟 | " + comList.get(1).getLanguage());
                        movieTheatres.clear();
                        movieTheatres.addAll(sessions.stream().filter(a -> a.getMovieId() == comList.get(1).getId()).collect(Collectors.toList()));
                        initAdapter(movieTheatres);
                    }
                });
                imgMovieTheatre7.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Glide.with(imgMovieTheatre5.getContext()).load(Http.base + comList.get(2).getCover()).into(imgMovieTheatre5);
                        Glide.with(imgMovieTheatre6.getContext()).load(Http.base + comList.get(0).getCover()).into(imgMovieTheatre6);
                        Glide.with(imgMovieTheatre7.getContext()).load(Http.base + comList.get(1).getCover()).into(imgMovieTheatre7);
                        tvMovieTheatre4.setText(comList.get(2).getName());
                        tvMovieTheatre5.setText(comList.get(2).getScore() + "分");
                        tvMovieTheatre6.setText(comList.get(2).getDuration() + "分钟 | " + comList.get(2).getLanguage());
                        movieTheatres.clear();
                        movieTheatres.addAll(sessions.stream().filter(a -> a.getMovieId() == comList.get(2).getId()).collect(Collectors.toList()));
                        initAdapter(movieTheatres);
                    }
                });
            }
        });
    }

    private void initAdapter(List<MovieSessions.RowsBean> movieTheatres) {
        rvAd = new RvAd<MovieSessions.RowsBean>(movieTheatres, R.layout.rv_theatre_film) {
            private TextView tvRvTheatre1, tvRvTheatre2, tvRvTheatre3, tvRvTheatre4, tvRvTheatre5, tvRvTheatre6;
            private void initView(View view) {
                tvRvTheatre1 = (TextView) view.findViewById(R.id.tvRvTheatre1);
                tvRvTheatre2 = (TextView) view.findViewById(R.id.tvRvTheatre2);
                tvRvTheatre3 = (TextView) view.findViewById(R.id.tvRvTheatre3);
                tvRvTheatre4 = (TextView) view.findViewById(R.id.tvRvTheatre4);
                tvRvTheatre5 = (TextView) view.findViewById(R.id.tvRvTheatre5);
                tvRvTheatre6 = (TextView) view.findViewById(R.id.tvRvTheatre6);
            }

            @Override
            protected void initR(View iv, MovieSessions.RowsBean rowsBean, int p) {
                initView(iv);
                tvRvTheatre1.setText(rowsBean.getStartTime());
                tvRvTheatre2.setText(rowsBean.getEndTime() + "散场");
                tvRvTheatre3.setText(rowsBean.getRoomName());
                tvRvTheatre4.setText("￥" + rowsBean.getPrice());
                int balance = rowsBean.getTotalNum() - rowsBean.getSaleNum();
                tvRvTheatre5.setText("余票：" + balance);
                tvRvTheatre6.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        movieRoomId = rowsBean.getRoomId();
                        movieMovieId = rowsBean.getMovieId();
                        movieTheaterId = rowsBean.getTheaterId();
                        movieTimesId = rowsBean.getId();
                        moviePrice = rowsBean.getPrice();
                        moviePlayDate = rowsBean.getPlayDate();
                        add(new MovieSeatFragment());
                    }
                });
            }
        };
        rvMovieTheatre.setAdapter(rvAd);
    }

    private void initView(View v) {
        tvMovieTheatre1 = (TextView) v.findViewById(R.id.tvMovieTheatre1);
        tvMovieTheatre2 = (TextView) v.findViewById(R.id.tvMovieTheatre2);
        imgMovieTheatre1 = (ImageView) v.findViewById(R.id.imgMovieTheatre1);
        lineMovieTheatre1 = (LinearLayout) v.findViewById(R.id.lineMovieTheatre1);
        imgMovieTheatre2 = (ImageView) v.findViewById(R.id.imgMovieTheatre2);
        imgMovieTheatre3 = (ImageView) v.findViewById(R.id.imgMovieTheatre3);
        lineMovieTheatre3 = (LinearLayout) v.findViewById(R.id.lineMovieTheatre3);
        imgMovieTheatre5 = (ImageView) v.findViewById(R.id.imgMovieTheatre5);
        imgMovieTheatre6 = (ImageView) v.findViewById(R.id.imgMovieTheatre6);
        imgMovieTheatre7 = (ImageView) v.findViewById(R.id.imgMovieTheatre7);
        lineMovieTheatre2 = (LinearLayout) v.findViewById(R.id.lineMovieTheatre2);
        imgMovieTheatre4 = (ImageView) v.findViewById(R.id.imgMovieTheatre4);
        tvMovieTheatre4 = (TextView) v.findViewById(R.id.tvMovieTheatre4);
        tvMovieTheatre5 = (TextView) v.findViewById(R.id.tvMovieTheatre5);
        tvMovieTheatre6 = (TextView) v.findViewById(R.id.tvMovieTheatre6);
        tvMovieTheatreDate1 = (TextView) v.findViewById(R.id.tvMovieTheatreDate1);
        tvMovieTheatreDate2 = (TextView) v.findViewById(R.id.tvMovieTheatreDate2);
        tvMovieTheatreDate3 = (TextView) v.findViewById(R.id.tvMovieTheatreDate3);
        rvMovieTheatre = (RecyclerView) v.findViewById(R.id.rvMovieTheatre);
    }
    public static Date stringToDate(String dateString){
        ParsePosition position = new ParsePosition(0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.parse(dateString,position);
    }
}
